package com.staff.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.common.util.AJAXResultMessage;
import com.staff.dao.StaffRewardRepository;
import com.staff.entity.Staff;
import com.staff.entity.StaffReward;
import com.staff.entity.dto.StaffRewardQueryDTO;

@Service
@Transactional
public class StaffRewardService implements IStaffRewardService {
	@Autowired
	private StaffRewardRepository staffRewardRepository;
	@Autowired
	private StaffRewardService staffRewardService;
	@Autowired
	private StaffService staffService;
	
	@Override
	public AJAXResultMessage saveOrUpdate(StaffReward staffReward) {
		staffRewardRepository.save(staffReward);
		
		return new AJAXResultMessage(true,"操作成功");
	}

	@Override
	public void delete(StaffReward staffReward) {
		staffReward=staffRewardRepository.findOne(staffReward.getId());
		if(staffReward!=null) {
			staffReward.setStaff(null);
			staffRewardRepository.save(staffReward);
		}
		staffRewardRepository.delete(staffReward);
	}

	@Override
	public StaffReward findOne(Long id) {
		return staffRewardRepository.findOne(id);
	}

	@Override
	public List<StaffReward> findAll() {
		return (List<StaffReward>) staffRewardRepository.findAll();
	}

	@Override
	public void delete(List<Long> ids) {
		List<StaffReward> staffRewardList = (List<StaffReward>)staffRewardRepository.findAll(ids);
		for(StaffReward staffReward : staffRewardList) {
			staffReward.setStaff(null);
			staffRewardRepository.save(staffReward);
		}
			staffRewardRepository.delete(staffRewardList);
		
	}

	@Override
	public Page<StaffRewardQueryDTO> findAll(Specification<StaffReward> spec, Pageable pageable) {
		//将员工履历page转化为员工履历DTOpage
		List<StaffRewardQueryDTO> results = new ArrayList<StaffRewardQueryDTO>();
		Page<StaffReward> page = staffRewardRepository.findAll(spec, pageable);
		for(StaffReward sc:page.getContent()) {
			StaffRewardQueryDTO scDTO = new StaffRewardQueryDTO(sc, sc.getStaff());
        	results.add(scDTO);
		}
		return new PageImpl<StaffRewardQueryDTO>(results, pageable, results.size());
	}

	@Override
	public AJAXResultMessage saveOrUpdate(StaffRewardQueryDTO staffRewardQueryDTO) {
		StaffReward sr = new StaffReward();
		if(staffService.findOne(staffRewardQueryDTO.getStaffNum())!= null) //判断员工是否存在数据库
		{
			if(staffRewardQueryDTO.getId()== null)
			{//新建奖惩
				Staff staff = staffService.findOne(staffRewardQueryDTO.getStaffNum());
				
				sr.setStaffNum(staffRewardQueryDTO.getStaffNum());
				sr.setStaffName(staff.getStaffName());
				sr.setRewAndPunContent(staffRewardQueryDTO.getRewAndPunContent());
				sr.setRewAndPunDate(staffRewardQueryDTO.getRewAndPunDate());
				sr.setRewAndPunTitle(staffRewardQueryDTO.getRewAndPunTitle());
				sr.setStaff(staff);
				
				return staffRewardService.saveOrUpdate(sr);
			}
			else
			{ //修改奖惩
				sr = staffRewardService.findOne(staffRewardQueryDTO.getId());
				
				sr.setRewAndPunContent(staffRewardQueryDTO.getRewAndPunContent());
				sr.setRewAndPunDate(staffRewardQueryDTO.getRewAndPunDate());
				sr.setRewAndPunTitle(staffRewardQueryDTO.getRewAndPunTitle());
				
				return staffRewardService.saveOrUpdate(sr);

			}
		}
		return new AJAXResultMessage(false,"该员工不存在");
	}

}
